/*
	image_manager.js
	Author: Jason Ganzhorn
*/

// how many tiles to a side of the terrain image? currently 16x16
var NTILES_HIGH = 16;
var NTILES_WIDE = 16;

// TODO: add remaining blocks
var __lookup_table = new Object();
__lookup_table["1"] = [1,0,"stone"];
__lookup_table["2"] = [3,0,"grass"];
__lookup_table["3"] = [2,0,"dirt"];
__lookup_table["4"] = [0,1,"cobblestone"];
__lookup_table["5"] = [4,0,"wooden plank"];
__lookup_table["7"] = [1,1,"bedrock"];
__lookup_table["9"] = [15,12,"stationary water"];
__lookup_table["11"] = [15,14,"stationary lava"];
__lookup_table["12"] = [2,1,"sand"];
__lookup_table["13"] = [3,1,"gravel"];
__lookup_table["14"] = [0,2,"gold ore"];
__lookup_table["15"] = [1,2,"iron ore"];
__lookup_table["16"] = [2,2,"coal ore"];
__lookup_table["17.0"] = [4,1,"wood"];
__lookup_table["17.1"] = [4,7,"dark wood"];
__lookup_table["17.2"] = [5,7,"birch"];
__lookup_table["18"] = [4,3,"leaves"];
__lookup_table["19"] = [0,3,"sponge"];
__lookup_table["20"] = [1,3,"glass"];
__lookup_table["21"] = [0,10,"lapis lazuli ore"];
__lookup_table["22"] = [0,9,"lapis lazuli block"];
__lookup_table["23"] = [14,2,"dispenser"];
__lookup_table["24"] = [0,12,"sandstone"];
__lookup_table["25"] = [10,4,"note block"];
__lookup_table["35.0"] = [0,4,"white wool"];
__lookup_table["35.1"] = [2,13,"orange wool"];
__lookup_table["35.2"] = [2,12,"magenta wool"];
__lookup_table["35.3"] = [2,11,"pale blue wool"];
__lookup_table["35.4"] = [2,10,"yellow wool"];
__lookup_table["35.5"] = [2,9,"lime green wool"];
__lookup_table["35.6"] = [2,8,"pink wool"];
__lookup_table["35.7"] = [2,7,"dark gray wool"];
__lookup_table["35.8"] = [1,14,"gray wool"];
__lookup_table["35.9"] = [1,13,"aqua wool"];
__lookup_table["35.10"] = [1,12,"purple wool"];
__lookup_table["35.11"] = [1,11,"blue wool"];
__lookup_table["35.12"] = [1,10,"brown wool"];
__lookup_table["35.13"] = [1,9,"dark green wool"];
__lookup_table["35.14"] = [1,8,"red wool"];
__lookup_table["35.15"] = [1,7,"black wool"];
__lookup_table["41"] = [7,1,"gold block"];
__lookup_table["42"] = [6,1,"iron block"];
__lookup_table["45"] = [7,0,"brick"];
__lookup_table["46"] = [8,0,"tnt"];
__lookup_table["47"] = [3,2,"bookshelf"];
__lookup_table["48"] = [4,2,"mossy cobblestone"];
__lookup_table["49"] = [5,2,"obsidian"];
__lookup_table["52"] = [1,4,"monster spawner"];
__lookup_table["54"] = [11,1,"chest"];
__lookup_table["56"] = [2,3,"diamond ore"];
__lookup_table["57"] = [8,1,"diamond block"];
__lookup_table["58"] = [12,3,"crafting table"];
__lookup_table["61"] = [12,2,"furnace"];
__lookup_table["73"] = [3,3,"redstone ore"];
__lookup_table["78"] = [2,4,"snow layer"];
__lookup_table["79"] = [3,4,"ice"];
__lookup_table["80"] = [2,4,"snow block"];
__lookup_table["81"] = [5,4,"cactus"];
__lookup_table["82"] = [8,4,"clay block"];
__lookup_table["84"] = [11,4,"jukebox"];
__lookup_table["85"] = ["fence"];
__lookup_table["86"] = [7,7,"pumpkin"];
__lookup_table["87"] = [7,6,"netherrack"];
__lookup_table["88"] = [8,6,"soul sand"];
__lookup_table["89"] = [9,6,"glowstone block"];
__lookup_table["91"] = [8,7,"jack-o-lantern"];
__lookup_table["92"] = [12,8,"cake block"];

function ImageManager(url) {
	this.image = new Element("img");
	this.image.manager = this;
	this.dims = {height: 16, width: 16};
	this.image.observe("load", function() {
		// set tile dimensions (this adds flexibility in the case of a resolution upgrade)
		this.manager.dims.height = this.height / NTILES_HIGH;
		this.manager.dims.width = this.width / NTILES_WIDE;
	});
	this.image.src = url;
}

ImageManager.prototype.setElementBackgroundByIndex = function(element, index) {
	if(__lookup_table[index] != undefined) {
		if(__lookup_table[index].length > 1) {
			var bg_x = -__lookup_table[index][0]*this.dims.width;
			var bg_y = -__lookup_table[index][1]*this.dims.height;

			element.setStyle({
				backgroundImage: "url(images/terrain.png)",
				backgroundPosition: bg_x + "px " + bg_y + "px",
				height: image_mgr.dims.height + "px",
				width: image_mgr.dims.width + "px"
			});
			
			if(__lookup_table[index][2] != undefined)
				element.up().title = __lookup_table[index][2];
		} else if(__lookup_table[index].length == 1)
			element.up().title = __lookup_table[index][0];
	} else {
		element.setStyle({
			backgroundImage: "none"
		});
		element.title = "";
	}
};